// pages/myclubmanage/myclubmanage.js

const db = wx.cloud.database();
const clubco = db.collection("club")
const user = db.collection('user')
const _ = db.command

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabarr: ["社团信息", "活动管理", "社团成员"],
    TabCur: 0,
    scrollLeft: 0,
    club: [],
    student: 100,
    male: 33,
    activeNames: ['1'],
    clubId: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      clubId: options.mymanageclubid
    })

    wx.setNavigationBarTitle({
      title: '我管理的社团',
    })

    this.getMember();
    this.getCheckMember();


    db.collection("event").where({

      host: options.mymanageclubid
    }).get().then(function (eveRes) {
      that.setData({
        events: eveRes.data
      })
    })
    clubco.where({
      _id: options.mymanageclubid
    }).get().then(
      function (res) {
        that.setData({
          club: res.data[0]
        })
      }
    )
    /*
    var ctx = wx.createCanvasContext('customCanvas')
    ctx.setFillStyle('#12CEDD')
    ctx.fillRect(10, 10, 280 * that.data.male / that.data.student, 20)
    ctx.draw()
    ctx.setFillStyle('pink')
    ctx.fillRect(10 + 280 * that.data.male / that.data.student, 10, 280 * (1 - that.data.male / that.data.student), 20)
    ctx.draw(true)
    */
  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  gotoCreateEvent: function (e) {
    var that = this
    wx.navigateTo({
      url: '../../pages/createvent/createvent?id=' + that.data.clubId
    })
  },





  gotoEdit: function () {
    var that = this
    wx.navigateTo({
      url: "../../pages/clubinfoedit/clubinfoedit?clubid=" + that.data.club._id
    })
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },


  getMember: function () {
    let that = this

    
    clubco.where({
        _id: that.data.clubId
      }).get()
      .then(res => {
        console.log('res data0 joinmember' + res.data[0].joinmember)

        user.where({                                     //已加入的成员人数
          _openid: _.in(res.data[0].joinmember)
        }).count()
        .then(res=>{
          that.setData({
            memberCounts:res.total
          })
         } )

        user.where({                                   //查询已加入的用户信息
          _openid: _.in(res.data[0].joinmember)
        }).get()
        .then(res => {
  
          that.setData({
            joinmember: res.data
          })
  
          console.log('user joinmember res' + JSON.stringify(res.data))
        })
  
      })

   
  },

  getCheckMember: function () {
    let that = this
    clubco.where({
        _id: that.data.clubId
      }).get()
      .then(res => {
        console.log('res data0 checkmember' + res.data[0].checkmember)
       
        user.where({                                     //待审核的成员人数
          _openid: _.in(res.data[0].checkmember)
        }).count()
        .then(res=>{
          that.setData({
            checkCounts:res.total
          })
         } )
     
        user.where({                                //查询待审核的用户信息
          _openid: _.in( res.data[0].checkmember)
        }).get()
        .then(res => {

          that.setData({
            checkmember: res.data
          })

          console.log('user checkmember res' + JSON.stringify(res.data))

        })

       
      })

  }


})